﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QLKBNT
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataTable sample;
        private void Form1_Load(object sender, EventArgs e)
        {
            textBoxX1.Focus();
            sample = new DataTable(); //Sample Data
            sample.Columns.Add("id", typeof(string));
            sample.Columns.Add("name", typeof(string));
            sample.Rows.Add("1", "apple");
            sample.Rows.Add("2", "acer");
            sample.Rows.Add("3", "alpha");
            sample.Rows.Add("4", "beat");
            sample.Rows.Add("5", "ball");
            sample.Rows.Add("6", "cat");
            sample.Rows.Add("7", "catch");
            sample.Rows.Add("10", "zebra");
            listView1.View = View.Details;
            listView1.Columns.Add("name");
            listView1.Columns.Add("id");
            addItems();
            textBox1.TextChanged += new EventHandler(textBox1_TextChanged);
            
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
            addItems();
        }

        private void addItems()
        {
            listView1.Items.Clear(); //Clear all the Data in the ListView
            foreach(DataRow row in sample.Rows)
            {
                if(row["name"].ToString().StartsWith(textBox1.Text)) //If the cell value is start  with the value in the TextBox
                {
                    ListViewItem item = new ListViewItem(row["name"].ToString());
                    item.SubItems.Add(row["id"].ToString());
                    listView1.Items.Add(item); //Add this row to the ListView
                }
            }
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            listView1.Visible = false;
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            listView1.Visible = true;
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            textBox1.Text = listView1.FocusedItem.Text ;
            listView1.Visible = false;
        }

        

           
       
    }
}
